package www.shuang.users.service.Impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;

import www.shuang.common.util.PageInfoUtil;
import www.shuang.users.dao.IAAdminsDao;
import www.shuang.users.dao.IAUsersCardDao;
import www.shuang.users.dao.IAUsersDao;
import www.shuang.users.dao.IAUsersGrantDao;
import www.shuang.users.pojo.AAdmins;
import www.shuang.users.pojo.AUsers;
import www.shuang.users.pojo.AUsersCard;
import www.shuang.users.pojo.AUsersGrant;
import www.shuang.users.service.IAAdminsService;

@Service("adminsService")
public class UsersServiceImpl implements IAAdminsService
{	
	@Resource
	private IAAdminsDao adminsDao;
	@Resource
	private IAUsersDao usersDao;
	@Resource
	private IAUsersGrantDao usersGrantDao;
	@Resource
	private IAUsersCardDao usersCardDao;
	@Override
	public JSONObject insertOneAAdminsService(AAdmins admins)
	{
			JSONObject resultJSON=new JSONObject();
			int res=this.adminsDao.insert(admins);
			//验证邮箱的唯一性，服务器端验证，service验证
			Map<String,Object> condMap=new HashMap<String,Object>();
			condMap.put("email", admins.getEmail());
			AAdmins adminsSert=this.findOneAAdminsService(condMap);
			if(adminsSert != null){
				resultJSON.put("code", "2");
				resultJSON.put("info", "邮箱已经存在，请更换邮箱");
				return resultJSON;
			}
			//事务处理机制
			/*String str=null;
			str.toString();*/
		if(res>0){
			resultJSON.put("code", "0");
			resultJSON.put("info", "添加成功");
			//添加二级json
			JSONObject dataJSON=new JSONObject();
			dataJSON.put("affect",res);
			resultJSON.put("data", dataJSON);
		}else{
			resultJSON.put("code", "1");
			resultJSON.put("info", "添加失败");
		}
		
		return resultJSON;
	}
	@Override
	public JSONObject updateOneAAdminsService(AAdmins admins)
	{
		JSONObject resultJSON=new JSONObject();
		int res=this.adminsDao.update(admins);
		//事务处理机制
		/*String str=null;
		str.toString();*/
	if(res>0){
		resultJSON.put("code", "0");
		resultJSON.put("info", "更新成功");
		//添加二级json
		JSONObject dataJSON=new JSONObject();
		dataJSON.put("affect",res);
		resultJSON.put("data", dataJSON);
	}else{
		resultJSON.put("code", "1");
		resultJSON.put("info", "更新失败");
	}
	
	return resultJSON;
	}
	@Override
	public JSONObject deleteOneAAdminsService(AAdmins admins)
	{
		JSONObject resultJSON=new JSONObject();
		int res=this.adminsDao.delete(admins);
		//事务处理机制
		/*String str=null;
		str.toString();*/
	if(res>0){
		resultJSON.put("code", "0");
		resultJSON.put("info", "删除成功");
		//添加二级json
		JSONObject dataJSON=new JSONObject();
		dataJSON.put("affect",res);
		resultJSON.put("data", dataJSON);
	}else{
		resultJSON.put("code", "1");
		resultJSON.put("info", "删除失败");
	}
	
	return resultJSON;
	}
	
	@Override
	public AAdmins findOneAAdminsService(Map<String, Object> condMap)
	{
		return this.adminsDao.findOne(condMap);
	}
	@Override
	public List<AAdmins> findCondListAAdminsService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		/*pageCount == true:拿总记录数:
		pageCount == false:分页数据:
		pageCount 为null不分页查询*/
		List<AAdmins> adminsList=new ArrayList<AAdmins>();
		//处理关键字
		if(condMap.get("keyword") != null){
			condMap.put("keyword", "%"+condMap.get("keyword")+"%");
		}
		if(pageInfoUtil != null){
			//分页查询
			condMap.put("pageCount", "true");
			adminsList=this.adminsDao.findCondList(condMap);
			//分页,总记录数放在了id属性上
			pageInfoUtil.setTotalRecord(adminsList.get(0).getId());
			//拿分页数据
			condMap.put("pageCount","false");
			//每页有多少记录
			condMap.put("currentRecord", pageInfoUtil.getCurrRecord());
			condMap.put("pageSize", pageInfoUtil.getPageSize());
			adminsList=this.adminsDao.findCondList(condMap);
			
		}else{
			adminsList=this.adminsDao.findCondList(condMap);
		}
		
		
		return adminsList;
	}
	@Override
	public AUsers findOneAUsersService(Map<String, Object> condMap)
	{
		return this.usersDao.findOne(condMap);
	}
	@Override
	public List<AUsers> findCondListAUsersService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		return this.usersDao.findCondList(condMap);
	}
	@Override
	public JSONObject insertOneAUsersService(AUsers users)
	{
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public JSONObject updateOneAUsersService(AUsers users)
	{
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public JSONObject deleteOneAUsersService(AUsers users)
	{
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public AUsersGrant findOneAUsersGrantService(Map<String, Object> condMap)
	{
		return this.usersGrantDao.findOne(condMap);
	}
	@Override
	public List<AUsers> findCondListAUsersGrantService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public JSONObject insertOneAUsersService(AUsersGrant usersGrant)
	{
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public JSONObject updateOneAUsersService(AUsersGrant usersGrant)
	{
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public JSONObject deleteOneAUsersService(AUsersGrant usersGrant)
	{
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public AUsersCard findOneUsersCardService(Map<String, Object> condMap)
	{
		
		return this.usersCardDao.findOne(condMap);
	}
	@Override
	public List<AUsersCard> findCondListUsersCardService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		List<AUsersCard> usersCardList = new ArrayList<AUsersCard>();
		/*
		 * 处理一下关键字
		 * keyword 变成: % + keyword + %
		 * */
		if(condMap.get("keyword") != null)
		{
			condMap.put("keyword", "%" + condMap.get("keyword") + "%");
		}
		
		/*pageCount == true:拿总记录数:
		pageCount == false:分页数据:
		pageCount 为null不分页查询*/
		if(pageInfoUtil != null)
		{
			//分页查询
			/* 先查询总记录数 */
			condMap.put("pageCount", "true");
			usersCardList = this.usersCardDao.findCondList(condMap);
			/* usersCardList有一条记录,总记录数放到了id属性上 */
			pageInfoUtil.setTotalRecord(usersCardList.get(0).getId());
			
			/* 拿分页数据 */
			condMap.put("pageCount", "false");
			/* 当前记录数
			 * 每页多少条 */
			condMap.put("currentRecord", pageInfoUtil.getCurrRecord());
			condMap.put("pageSize", pageInfoUtil.getPageSize());
			usersCardList = this.usersCardDao.findCondList(condMap);
		}else
		{
			//不分页查询
			usersCardList = this.usersCardDao.findCondList(condMap);
		}
		return usersCardList;
	}
	

}
